/// <reference path="./global.d.ts" />
require(['vue', 'msg', 'storage'], function (vue, msg, storage) {
    /**
     * 地图坐标点角色
     */
    enum Role {
        /**
         * 空白
         */
        Blank,
        /**
         * 墙
         */
        Wall,
        /**
         * 小人
         */
        People,
        /**
         * 小人点
         */
        PeopleDot,
        /**
         * 箱子
         */
        Chest,
        /**
         * 箱子点
         */
        Dot,
        /**
         * 到点箱子
         */
        ChestDot

    }

    /**
     * 移动位置
     */
    enum MovePotion {
        Up,
        Down,
        Left,
        Right
    }

    interface Coord {
        X: number,
        Y: number
    }

    class People {
        private currentCoord: Coord;
        constructor(coord: Coord) {
            this.currentCoord = coord;
        }

        private getNewCoord(oldCoord: Coord, potion: MovePotion): Coord {
            let newCoord: Coord = {
                Y: oldCoord.Y,
                X: oldCoord.X
            }
            switch (potion) {
                case MovePotion.Up:
                    newCoord.Y--;
                    break;
                case MovePotion.Right:
                    newCoord.X++;
                    break;
                case MovePotion.Down:
                    newCoord.Y++;
                    break;
                case MovePotion.Left:
                    newCoord.X--;
                    break;
            }

            return newCoord;
        }

        setCoord(coord: Coord) {
            this.currentCoord.Y = coord.Y;
            this.currentCoord.X = coord.X;
        }

        move(potion: MovePotion) {
            let peopleNewCoord: Coord = this.getNewCoord(this.currentCoord, potion);
            let currentCoordRole: Role = game.getCoordRole(this.currentCoord);
            let newCoordRole: Role = game.getCoordRole(peopleNewCoord);
            if (!(newCoordRole == Role.Blank || newCoordRole == Role.Chest || newCoordRole == Role.Dot || newCoordRole == Role.ChestDot))
                return;

            let updateCurrentCoord = () => {
                this.currentCoord.Y = peopleNewCoord.Y;
                this.currentCoord.X = peopleNewCoord.X;
                game.addPace();
            }

            if (newCoordRole == Role.Blank) {
                game.setCoordRole(peopleNewCoord, Role.People);
                game.setCoordRole(this.currentCoord, currentCoordRole == Role.PeopleDot ? Role.Dot : Role.Blank);
                updateCurrentCoord();
            } else if (newCoordRole == Role.Dot) {
                game.setCoordRole(peopleNewCoord, Role.PeopleDot);
                game.setCoordRole(this.currentCoord, currentCoordRole == Role.PeopleDot ? Role.Dot : Role.Blank);
                updateCurrentCoord();
            } else if (newCoordRole == Role.Chest || newCoordRole == Role.ChestDot) {
                let newChestCoord: Coord = this.getNewCoord(peopleNewCoord, potion);
                let newChestCoordRole: Role = game.getCoordRole(newChestCoord);
                if (newChestCoordRole == Role.Wall || newChestCoordRole == Role.ChestDot || newChestCoordRole == Role.Chest)
                    return;

                game.setCoordRole(newChestCoord, newChestCoordRole == Role.Dot ? Role.ChestDot : Role.Chest);
                game.setCoordRole(peopleNewCoord, newCoordRole == Role.ChestDot ? Role.PeopleDot : Role.People);
                game.setCoordRole(this.currentCoord, currentCoordRole == Role.PeopleDot ? Role.Dot : Role.Blank);
                updateCurrentCoord();
            }

            game.verifySuccess();
        }
    }

    class Map {
        private level: number = 0;
        private levelMap: string[] = [
            '[[0,0,0,0,0,0,0,0,0,0],[0,0,0,0,1,1,1,0,0,0],[0,0,0,0,1,5,1,0,0,0],[0,1,1,1,1,0,1,0,0,0],[0,1,5,4,0,4,1,1,1,0],[0,1,1,1,2,4,0,5,1,0],[0,0,0,1,4,1,1,1,1,0],[0,0,0,1,5,1,0,0,0,0],[0,0,0,1,1,1,0,0,0,0],[0,0,0,0,0,0,0,0,0,0],[0,0,0,0,0,0,0,0,0,0],[0,0,0,0,0,0,0,0,0,0]]',
            '[[0,0,0,0,0,0,0,0,0,0],[0,1,1,1,1,1,0,0,0,0],[0,1,2,0,0,1,0,0,0,0],[0,1,0,4,4,1,0,1,1,1],[0,1,0,4,0,1,0,1,5,1],[0,1,1,1,0,1,1,1,5,1],[0,0,1,1,0,0,0,0,5,1],[0,0,1,0,0,0,1,0,0,1],[0,0,1,0,0,0,1,1,1,1],[0,0,1,1,1,1,1,0,0,0],[0,0,0,0,0,0,0,0,0,0],[0,0,0,0,0,0,0,0,0,0]]',
            '[[0,0,0,0,0,0,0,0,0,0],[0,0,0,0,0,0,0,0,0,0],[0,1,1,1,1,1,1,1,0,0],[0,1,0,0,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0]]'
        ];
        constructor() {
            this.level = Number(storage.get('chestMapLevel')) || 0;
        }

        private setLevel(level: number): void {
            if (level < 0)
                level = 0;
            else if (level >= this.levelMap.length)
                level = this.levelMap.length - 1;

            this.level = level;
            storage.add('chestMapLevel', level.toString());
        }

        getMapData(): number[][] {
            if (this.level + 1 > this.levelMap.length)
                return [];

            return JSON.parse(this.levelMap[this.level]);
        }

        getNextLevelMapData(): number[][] {
            this.setLevel(this.level + 1);
            return this.getMapData();
        }

        getPrevLevelMapData(): number[][] {
            this.setLevel(this.level - 1);
            return this.getMapData();
        }

        getLevel(): number {
            return this.level;
        }

        getTotalLevel(): number {
            return this.levelMap.length;
        }

        getPeopleInitCoord(): Coord {
            let mapData: number[][] = this.getMapData();
            for (var y = 0; y < mapData.length; y++) {
                for (var x = 0; x < mapData[y].length; x++) {
                    if (mapData[y][x] == Role.People) {
                        return {
                            Y: y,
                            X: x
                        }
                    }
                }
            }

            return {
                Y: 0,
                X: 0
            }
        }

        getInitMapData(yLen: number, xLen: number): number[][] {
            /**
             * 获取地图坐标点的角色
             * @param y 坐标y
             * @param x 坐标x
             */
            function getMapRole(y: number, x: number): Role {
                return Role.Blank;
            }


            let tempMap: number[][] = [];
            for (var i = 0; i < yLen; i++) {
                let tempXMap: number[] = [];
                for (var z = 0; z < xLen; z++) {
                    tempXMap.push(getMapRole(i, z));
                }
                tempMap.push(tempXMap);
            }

            return tempMap;
        }
        reset() {
            this.setLevel(0);
        }
    }

    class Game {
        private vueObj: any;
        private isOver: boolean = false;
        constructor(mapData: number[][]) {
            let gameThis = this;

            this.vueObj = new vue({
                el: '#app',
                data: {
                    Map: mapData,
                    IsDesign: false,
                    IsLook: false,
                    Level: map.getLevel() + 1,
                    Pace: 0
                },
                methods: {
                    getRoleClass(role: number): string {
                        let classNames: string[] = ['mapItem'];
                        switch (role) {
                            case Role.Wall:
                                classNames.push('wall');
                                break;
                            case Role.People:
                            case Role.PeopleDot:
                                classNames.push('people');
                                break;
                            case Role.Chest:
                            case Role.ChestDot:
                                classNames.push('chest');
                                break;
                            case Role.Dot:
                                classNames.push('dot');
                                break;
                        }

                        return classNames.join(' ');
                    },
                    SetRole(y: number, x: number) {
                        if (this.IsDesign) {
                            let role: Role = this.Map[y][x];
                            let newRole = role + 1;
                            if (newRole == Role.PeopleDot)
                                newRole++;


                            if (newRole > Role.ChestDot) {
                                newRole = Role.Blank;
                            }

                            this.$set(this.Map[y], x, newRole);
                            console.info(JSON.stringify(this.Map));
                        }
                    },
                    Up() {
                        if (this.IsLook)
                            gameThis.prevLevel();
                        else
                            if (!this.isOver) {
                                people.move(MovePotion.Up);
                            }
                    },
                    Down() {
                        if (this.IsLook)
                            gameThis.nextLevel();
                        else
                            if (!this.isOver)
                                people.move(MovePotion.Down);
                    },
                    Left() {
                        if (this.IsLook)
                            gameThis.prevLevel();

                        else
                            if (!this.isOver)
                                people.move(MovePotion.Left);
                    },
                    Right() {
                        if (this.IsLook)
                            gameThis.nextLevel();
                        else
                            if (!this.isOver)
                                people.move(MovePotion.Right);
                    },
                    Reload() {
                        this.Pace = 0;
                        this.Map = map.getMapData();
                        people.setCoord(map.getPeopleInitCoord());
                    },
                    Reset() {
                        map.reset();
                        this.Reload();
                        this.Pace = 0;
                        this.Level = map.getLevel() + 1;
                        gameThis.showLevelName();
                    },
                    Empty() {
                        this.IsDesign = true;
                        this.Map = map.getInitMapData(12, 10);
                    },
                    Look() {
                        if (!this.IsLook) {
                            msg.msg('按方向键选择关卡');
                            this.IsLook = true;
                        } else {
                            msg.msg('取消选择关卡');
                            this.IsLook = false;
                        }
                    }
                },
                mounted() {
                    gameThis.showLevelName();
                }
            });
        }

        getCoordRole(coord: Coord): Role {
            return this.vueObj.Map[coord.Y][coord.X];
        }

        setCoordRole(coord: Coord, role: Role): void {
            this.vueObj.$set(this.vueObj.Map[coord.Y], coord.X, role);
        }

        verifySuccess(): void {
            let isPass: boolean = true;
            for (var y = 0; y < this.vueObj.Map.length; y++) {
                for (var x = 0; x < this.vueObj.Map[y].length; x++) {
                    let role: Role = this.vueObj.Map[y][x];
                    if (role == Role.Dot || role == Role.PeopleDot) {
                        isPass = false;
                        break;
                    }
                }
            }

            if (isPass) {
                if (map.getLevel() + 1 == map.getTotalLevel()) {
                    this.isOver = true;
                    msg.msg('恭喜通关！撒花撒花！');
                }
                else {
                    msg.msg('恭喜通过第' + (map.getLevel() + 1) + '关，共计用了' + this.vueObj.Pace + '步，即将进入下一关');
                    setTimeout(() => {
                        this.nextLevel();
                    }, 2000);                    
                }
            }
        }



        nextLevel(): void {
            this.initLevelData(map.getNextLevelMapData());
        }

        prevLevel(): void {
            this.initLevelData(map.getPrevLevelMapData());
        }

        initLevelData(mapData: number[][]): void {
            this.vueObj.Level = map.getLevel() + 1;
            this.vueObj.Map = mapData;
            people.setCoord(map.getPeopleInitCoord());
            this.showLevelName();
            this.resetPace();
        }

        showLevelName(): void {
            msg.msg('进入第' + (map.getLevel() + 1) + '关');
        }

        addPace(): void {
            this.vueObj.Pace++;
        }

        resetPace(): void {
            this.vueObj.Pace = 0;
        }

    }

    var map = new Map();
    var people = new People(map.getPeopleInitCoord());
    var game = new Game(map.getMapData());

    window.addEventListener('keyup', function (e) {
        switch (e.keyCode) {
            case 38:
                people.move(MovePotion.Up);
                break;
            case 40:
                people.move(MovePotion.Down);
                break;
            case 37:
                people.move(MovePotion.Left);
                break;
            case 39:
                people.move(MovePotion.Right);
                break;
        }
    });
});

